#include <cstdlib>
#include <string>
#include <iostream>
#include <set>
#include <vector>
using namespace std;



// Definition for singly-linked list.
struct ListNode {
     int val;
     ListNode *next;
     ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
	ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
		int numA = 0;
		int numB = 0;
		ListNode* nodeA = headA;
		ListNode* nodeB = headB;
		while (nodeA != NULL){
			nodeA = nodeA->next;
			++numA;
		}
		while (nodeB != NULL){
			nodeB = nodeB->next;
			++numB;
		}
		nodeA = headA;
		nodeB = headB;
		if (numA > numB){
			for (int i = 0; i < numA - numB; ++i)
				nodeA = nodeA->next;
		}
		else if (numA < numB){
			for (int i = 0; i < numB - numA; ++i)
				nodeB = nodeB->next;
		}
		bool hasIntersect = false;
		while (nodeA != NULL && nodeB != NULL){
			if (nodeA == nodeB)
				hasIntersect = true;
			nodeA = nodeA->next;
			nodeB = nodeB->next;
		}
		if (hasIntersect)
			return nodeA;
		else return NULL;
	}
};

int main(int argc, char** argv){
	ListNode node(1);
	Solution().getIntersectionNode(&node, &node);
	//Solution().getIntersectionNode(headA, headB);
	//cout << Solution().compareVersion("1.1.1", "1.1") << endl;
	return 0;
}